home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3302 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.1 KB  |  81 lines

  1. Path: EU.net!sun4nl!ittpub!ittpub!nntp
  2. Newsgroups: comp.lang.c++
  3. Subject: Re: GCC and Default Constructors
  4. Message-ID: <1996Jan23.105906.1752@ittpub>
  5. From: wil@ittpub.nl (Wil Evers)
  6. Date: 23 Jan 96 10:59:06 WET
  7. References: <4dtv71$1eh@news1.usa.pipeline.com>
  8. Distribution: world
  9. Nntp-Posting-Host: lintilla
  10.  
  11. In article <4dtv71$1eh@news1.usa.pipeline.com> grantp@usa.pipeline.com  
  12. writes:
  13. > On Jan 20, 1996 17:29:29 in article <GCC and Default Constructors>, 
  14. > 'Eric Richard <erichard@netgen.com>' wrote: 
  15. >   
  16. > >I just spent about 3 days intermittently trying to solve a problem that 
  17. > >I finally figured out and I was trying to see if there is something  
  18. > >I can do to avoid a similar situation. 
  19. > > 
  20. > >Basically my problem stemmed from a typo on my part and the fact 
  21. > >that C++ will automatically provide a default constructor for you if 
  22. > >you don't provide one.  To make a long story short, I had a class 
  23. > >named 'QueryTable' and had defined a constructor
  24. > >'queryTable::QueryTable'.
  25. > >Now obviously this is wrong, but what was screwing me up was that no 
  26. > >error was being generated because G++ was happily providing me with 
  27. > >a default constructor for my class. 
  28. > Are you saying that the compiler accepted a function definition 
  29. > for an undeclared class???  Any decent compiler -- and gcc/g++  
  30. > certainly is decent -- should not only warn you, but refuse to compile 
  31. > the file.  Maybe you've left out something relevant.  Or maybe you 
  32. > have another typo.
  33.  
  34. However, a similar error sometimes bites me:
  35.  
  36. class ClassName {
  37. public :
  38.     Classname(const ClassName&) { ... }
  39.     //   ^--- lower case 'n': not a copy constructor, 
  40.     //   but a regular member function
  41. };
  42.  
  43. > >So, my question is this:  Does anybody know if there is some way 
  44. > >of at least getting G++ to warn you if it is using a default 
  45. > >constructor? If I had that, I would've been able to figure this out
  46. > >much more quickly. 
  47. > > 
  48. > No, there isn't, and I'm not convinced that there ought to be one. 
  49. > Like the old saw:  Too many warnings spoil the stew -- or maybe I 
  50. > now have a typo :-).   
  51. >  
  52. > Excessive warnings defeat their purpose.  IMHO, warnings about 
  53. > default constructors borderlines on the excessive.
  54.  
  55. Agreed, such a warning would be very annoying, especially when including  
  56. perfectly valid class definitions written by other people. However, as a  
  57. matter of routine, I always consider what should happen if someone tries  
  58. to copy an object of a class I wrote myself, and I make sure my classes  
  59. either have a defined copy constructor and assignment operator (even when  
  60. the compiler would have generated the same code), or I disable them by  
  61. declaring them private.
  62.  
  63. Sometimes, when in a paranoid mood, I use the following helper class:
  64.  
  65.     class NoGeneratedCopying {
  66.     private :
  67.         NoGeneratedCopying(const NoGeneratedCopying&);
  68.         void operator=(const NoGeneratedCopying&);
  69.     public :
  70.         NoGeneratedCopying() { }
  71.     };
  72.  
  73. If you put a 'NoGeneratedCopying' data member in a class, the compiler is  
  74. not allowed to generate a copy constructor or assignment operator for that  
  75. class, and unless you explicitly provide them, an attempt to copy an  
  76. object of that class will result in a compile-time error.
  77.  
  78. - Wil
  79.